package com.itany.shop.controller;

import com.itany.shop.entity.Item;
import com.itany.shop.entity.Order;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.OrderService;
import com.itany.shop.service.ProductService;
import com.itany.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @author MissChen
 * @date 2024年04月01日 10:56
 */
public class MainController {
    //当前登录的用户
    private User user;
    //当用户成功登录进来之后，给用户一个购物车
    private Cart cart = new Cart();
    private Scanner sc = new Scanner(System.in);

    private ProductService productService = (ProductService) ObjectFactory.getObject("productService");
    private CartService cartService = (CartService) ObjectFactory.getObject("cartService");
    private OrderService orderService = (OrderService) ObjectFactory.getObject("orderService");

    public MainController(User user) {
        this.user = user;
    }

    public void showMenu() {
        System.out.println("-----------欢迎来到登录商城管理系统------------");
        System.out.print("请选择服务(1.加入购物车 2.查看购物车  3.我的订单 4.注销 5.修改密码)");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
            case 3:
                findMyOrders();
                break;
            case 4:
                logout();
                break;
        }
    }

    public void addCart() {
        List<Product> products = productService.findAll();
        if (!products.isEmpty()) {
            System.out.println("商品编号\t商品名称\t商品单价");
            for (Product product : products) {
                System.out.println(product.getId() + "\t" + product.getName() + "\t" + product.getPrice());
            }
        }
        System.out.print("请输入需要加入购物车的商品编号:");
        int id = sc.nextInt();
        try {
            cartService.addCart(cart, id);
            System.out.println("提示:添加购物车成功!");
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            System.out.println("添加购物车失败，失败原因:" + e.getMessage());
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            System.out.println("添加购物车失败，失败原因:" + e.getMessage());
        } catch (ProductNotExistException e) {
            e.printStackTrace();
            System.out.println("添加购物车失败，失败原因:" + e.getMessage());
        }
        showMenu();

    }

    public void showCart() {
        System.out.println("商品编号\t商品名称\t商品数量\t总价");
        List<Item> items = cart.getItems();

        for (Item item : items) {
            System.out.println(item.getProduct().getId() + "\t" + item.getProduct().getName() + "\t" + item.getNum() + "\t" + item.getPrice());
        }
        System.out.println("购物车总价:" + cart.getPrice());
        System.out.print("请选择:(1.删除商品 2.修改商品数量 3.清空购物车 4.结算 5.返回主菜单)");
        int choice = sc.nextInt();
        switch (choice) {
            case 1:
                removeCart();
                break;
            case 2:
                modifyCart();
                break;
            case 3:
                cleanCart();
                break;
            case 4:
                createOrder();
                break;
            case 5:
                showMenu();
        }
    }

    public void removeCart() {
        System.out.println("请输入需要删除的商品编号:");
        int id = sc.nextInt();
        try {
            cartService.removeCart(cart, id);
        } catch (UserNotLoginException e) {
            System.out.println("添加失败，原因：" + e.getMessage());
        } catch (RequestParameterErrorException e) {
            System.out.println("添加失败，原因：" + e.getMessage());
        }
        showMenu();
    }

    public void modifyCart() {
        System.out.println("请输入需要修改的商品编号:");
        int id = sc.nextInt();
        System.out.println("请输入修改后的数量:");
        int num = sc.nextInt();

        try {
            cartService.modifyCart(cart, id, num);
            System.out.println("提示:恭喜您，修改成功！");
        } catch (UserNotLoginException e) {
            System.out.println("对不起，修改失败:" + e.getMessage());
        } catch (RequestParameterErrorException e) {
            System.out.println("对不起，修改失败:" + e.getMessage());
        }
        showMenu();
    }

    public void cleanCart() {
        cart = new Cart();
        showCart();
    }

    public void createOrder() {
        try {
            String no = orderService.createOrder(cart, user);

            //结算成功后，清空当前结算的购物车
            cart = new Cart();
            System.out.println("提示：结算成功，订单号为:" + no);
        } catch (UserNotLoginException e) {
            System.out.println("错误：结算失败，失败原因:" + e.getMessage());
        }

        showMenu();
    }

    public void findMyOrders() {
        try {
            List<Order> orders = orderService.findMyOrders(user);
            if (!orders.isEmpty()) {
                System.out.println("订单号\t\t订单编号\t\t订单总价");
                for (Order order : orders) {
                    System.out.println(order.getId() + "\t" + order.getNo() + "\t" + order.getPrice());
                }
                System.out.print("请选择服务(1.查询订单详情 2.返回主菜单)");
                int choice = sc.nextInt();
                switch (choice) {
                    case 1:
                        findOrderDetail();
                    case 2:
                        showMenu();
                        break;
                    default:
                        System.out.println("您输入的选项有误,请重新输入");
                        findMyOrders();
                        break;
                }
            }
        } catch (UserNotExistException e) {
            System.out.println("失败原因:" + e.getMessage());
        }

    }
    public void findOrderDetail(){
        // 用户名:xxx
        // 手机号:xxx
        // 订单号:xxx
        // -----------------------------
        // 序号  商品名称   数量   总价
        //  1      aaa       2     5
        //  2      bbb       1     4
        //  3      ccc       3     12
        // ----------------------------
        // 共21元，共3条
        System.out.print("请输入需要查看的订单的id编号:");
        Integer id=sc.nextInt();
        try {
            Order order=orderService.findOrderDetail(id);
            System.out.println("用户名:"+order.getUser().getUsername());
            System.out.println("手机号:"+order.getUser().getPhone());
            System.out.println("订单号:"+order.getNo());
            System.out.println("------------------------------------------");
            List<Item> items=order.getItems();
            System.out.println("序号\t\t商品名称\t\t数量\t\t总价");
            for(int i=0;i<items.size();i++){
                Item item=items.get(i);
                System.out.println((i+1)+"\t\t"+item.getProduct().getName()+"\t\t"+item.getNum()+"\t\t"+item.getPrice());
            }
            System.out.println("共:"+order.getPrice()+"元,共"+order.getCount()+"条");
            System.out.print("请选择服务(1.返回我的订单 2.返回主菜单)");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                    findMyOrders();
                    break;
                case 2:
                    showMenu();
                    break;
                default:
                    findOrderDetail();
                    break;
            }
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
        }

    }
    public void logout(){
        new UserController().showSystemMenu();
    }

}
